package com.infitecs.eyas.nature.service;

import com.infitecs.eyas.boiler.dao.EfficiencyDao;
import com.infitecs.eyas.boiler.model.Efficiency;
import com.infitecs.eyas.boiler.service.EfficiencyService;
import com.infitecs.eyas.combustion.dao.BurnDao;
import com.infitecs.eyas.combustion.dao.ChimneyDao;
import com.infitecs.eyas.combustion.dao.CleanDao;
import com.infitecs.eyas.combustion.model.CalculateBurn;
import com.infitecs.eyas.combustion.model.CalculateChimney;
import com.infitecs.eyas.combustion.model.CalculateClean;
import com.infitecs.eyas.combustion.service.BurnService;
import com.infitecs.eyas.combustion.service.ChimneyService;
import com.infitecs.eyas.combustion.service.CleanService;
import com.infitecs.eyas.expression.dao.ExpressionDao;
import com.infitecs.eyas.expression.model.Formula;
import com.infitecs.eyas.mill.dao.MillSelectDao;
import com.infitecs.eyas.mill.dao.PipeCalculateDao;
import com.infitecs.eyas.mill.model.MillSelect;
import com.infitecs.eyas.mill.model.PipeCalculate;
import com.infitecs.eyas.mill.service.MillSelectService;
import com.infitecs.eyas.mill.service.PipeCalculateService;
import com.infitecs.eyas.nature.dao.NatureDao;
import com.infitecs.eyas.nature.model.Help;
import com.infitecs.eyas.nature.model.Nature;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.util.DoubleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xidea.el.Expression;
import org.xidea.el.impl.ExpressionImpl;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Demon on 2015/5/7.
 */
@Service("natureService")
public class NatureServiceImp implements NatureService {
    @Autowired
    private NatureDao natureDao;
    @Autowired
    private ExpressionDao expressionDao;
    @Autowired
    private OperateFieldDao operateFieldDao;
    @Autowired
    private EfficiencyDao efficiencyDao;
    @Autowired
    private EfficiencyService efficiencyService;
    @Autowired
    private BurnService burnService;
    @Autowired
    private BurnDao burnDao;
    @Autowired
    private CleanService cleanService;
    @Autowired
    private CleanDao cleanDao;
    @Autowired
    private ChimneyDao chimneyDao;
    @Autowired
    private ChimneyService chimneyService;
    @Autowired
    private MillSelectDao millSelectDao;
    @Autowired
    private MillSelectService millSelectService;
    @Autowired
    private PipeCalculateService pipeCalculateService;
    @Autowired
    private PipeCalculateDao pipeCalculateDao;
    @Override
    public HashMap calculateLocalPressureByFormula21(int powerId) {
        Nature nature = natureDao.getNatureByPowerId(powerId);
        Formula formula21 = expressionDao.getFormulaByName("公式21");
        Expression expression = new ExpressionImpl(formula21.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E3", nature.getAltitude());
        params.put("E10", nature.getAnnualAverageTemperature());
        double result1 = Double.valueOf(expression.evaluate(params).toString());
        double localPressure = 101.325 / Math.pow(10, result1);
        localPressure = DoubleUtil.getInstance().get3Double(localPressure);
        natureDao.updateLocalPressure(localPressure, powerId);

        Formula formula22 = expressionDao.getFormulaByName("公式22");
        Expression expression1 = new ExpressionImpl(formula22.getFormulaDescription());
        HashMap<String, Double> params1 = new HashMap<>();
        params1.put("E4", localPressure);
        params1.put("E6", 101.325);
        params1.put("E7", 760.0);
        double LocalPressureOther = Double.valueOf(expression1.evaluate(params1).toString());
        LocalPressureOther = DoubleUtil.getInstance().get3Double(LocalPressureOther);
        natureDao.updateLocalPressureOther(LocalPressureOther, powerId);

        HashMap<String, Double> result = new HashMap<>();
        result.put("localPressure", localPressure);
        result.put("LocalPressureOther", LocalPressureOther);
        return result;
    }

    @Override
    public double calculateLocalPressureOtherByFormula22(int powerId) {
        Nature nature = natureDao.getNatureByPowerId(powerId);
        Formula formula22 = expressionDao.getFormulaByName("公式22");
        Expression expression = new ExpressionImpl(formula22.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E4", nature.getLocalPressure());
        double LocalPressureOther = Double.valueOf(expression.evaluate(params).toString());
        return LocalPressureOther;
    }

    @Override
    public double calculateAnnualAverageAirMoistureByFormula23(int powerId) {
        Nature nature = natureDao.getNatureByPowerId(powerId);
        Formula formula23 = expressionDao.getFormulaByName("公式23");
        Expression expression = new ExpressionImpl(formula23.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E4", nature.getLocalPressure());
        params.put("E10", nature.getAnnualAverageTemperature());
        params.put("E13", nature.getAnnualAverageRelativeHumidity());
        double E14 = Double.valueOf(expression.evaluate(params).toString());
        E14 = DoubleUtil.getInstance().get4Double(E14);
        natureDao.updateAnnualAverageAirMoisture(E14, powerId);
        return E14;
    }

    @Override
    public double calculateLocalDensityMoistAirByFormula24(int powerId) {
        Nature nature = natureDao.getNatureByPowerId(powerId);
        Formula formula24 = expressionDao.getFormulaByName("公式24");
        Expression expression = new ExpressionImpl(formula24.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E14", nature.getAnnualAverageAirMoisture());
        double LocalDensityMoistAir = Double.valueOf(expression.evaluate(params).toString());
        LocalDensityMoistAir = DoubleUtil.getInstance().get3Double(LocalDensityMoistAir);
        natureDao.updateLocalDensityMoistAir(LocalDensityMoistAir, powerId);
        return LocalDensityMoistAir;
    }

    @Override
    public void updateAltitude(double altitude, int powerId) {
        natureDao.updateAltitude(altitude, powerId);
    }

    @Override
    public void updateAnnualAverageTemperature(double annualAverageTemperature, int powerId) {
        natureDao.updateAnnualAverageTemperature(annualAverageTemperature, powerId);
    }

    @Override
    public void updateLocalPressure(double localPressure, int powerId) {
        natureDao.updateLocalPressure(localPressure, powerId);
    }

    @Override
    public void updateLocalPressureOther(double localPressureOther, int powerId) {
        natureDao.updateLocalPressureOther(localPressureOther, powerId);
    }

    @Override
    public void updateOutdoorCalculatedTemperature(double outdoorCalculatedTemperature, int powerId) {
        natureDao.updateOutdoorCalculatedTemperature(outdoorCalculatedTemperature, powerId);
    }

    @Override
    public void updateSummerOutdoorCalculateTemperature(double summerOutdoorCalculateTemperature, int powerId) {
        natureDao.updateSummerOutdoorCalculateTemperature(summerOutdoorCalculateTemperature, powerId);
    }

    @Override
    public void updateAnnualAverageRelativeHumidity(double annualAverageRelativeHumidity, int powerId) {
        natureDao.updateAnnualAverageRelativeHumidity(annualAverageRelativeHumidity, powerId);
    }

    @Override
    public void updateMinDailyAverageTemperature(double minDailyAverageTemperature, int powerId) {
        natureDao.updateMinDailyAverageTemperature(minDailyAverageTemperature, powerId);
    }

    @Override
    public void updateAnnualAverageAirMoisture(double annualAverageAirMoisture, int powerId) {
        natureDao.updateAnnualAverageAirMoisture(annualAverageAirMoisture, powerId);
    }

    @Override
    public void updateLocalDensityMoistAir(double localDensityMoistAir, int powerId) {
        natureDao.updateLocalDensityMoistAir(localDensityMoistAir, powerId);
    }

    @Override
    public Nature getNatureByPowerId(int powerId) {
        return natureDao.getNatureByPowerId(powerId);
    }

    @Override
    public Help getHelp(String cityName) {
        return natureDao.getHelp(cityName);
    }

    @Override
    public List<Help> getAllDictNa() {
        return natureDao.getAllDictNa();
    }

    @Override
    public void natureSave(Nature nature) {
        natureSaveData(nature);
        //更新锅炉效率及燃煤量
        Efficiency oldEfficiency = efficiencyDao.getEfficiencyByPowerCaseId(nature.getPowerCaseId());
        OperateField operateField = operateFieldDao.getOperateField(nature.getPowerCaseId());
        if (oldEfficiency != null) {
            oldEfficiency.setE52State(operateField.getE52State());
            oldEfficiency.setE89State(operateField.getE89State());
            oldEfficiency.setE90State(operateField.getE90State());
            oldEfficiency.setE92State(operateField.getE92State());
            oldEfficiency.setE93State(operateField.getE93State());
            oldEfficiency.setE94State(operateField.getE94State());
            oldEfficiency.setE95State(operateField.getE95State());
            efficiencyService.efficiencySaveData(oldEfficiency);
        }

        //更新燃烧
        CalculateBurn calculateBurn = burnDao.getCalculateBurnByCaseId(nature.getPowerCaseId());
        if (calculateBurn != null) {
            calculateBurn.setYyglState(operateField.getYyglState());
            calculateBurn.setE54State(operateField.getE54State());
            calculateBurn.setE88State(operateField.getE88State());
            calculateBurn.setE122State(operateField.getE122State());
            calculateBurn.setE155State(operateField.getE155State());
            calculateBurn.setE171State(operateField.getE171State());
            burnService.burnSaveData(calculateBurn);
        }
        //更新烟气净化系统
        CalculateClean calculateClean = cleanDao.getCalculateClean(nature.getPowerCaseId());
        if (calculateClean != null) {
            calculateClean.setE221State(operateField.getE221State());
            calculateClean.setE222State(operateField.getE222State());
            calculateClean.setE231State(operateField.getE231State());
            calculateClean.setE241State(operateField.getE241State());
            calculateClean.setE242State(operateField.getE242State());
            calculateClean.setE248State(operateField.getE248State());
            calculateClean.setE258State(operateField.getE258State());
            cleanService.cleanDataSave(calculateClean);
        }
        //更新烟囱出口直径计算
        CalculateChimney calculateChimney = chimneyDao.getCalculateChimney(nature.getPowerCaseId());
        if (calculateChimney != null) {
            calculateChimney.setE314State(operateField.getE314State());
            calculateChimney.setE315State(operateField.getE315State());
            calculateChimney.setE316State(operateField.getE316State());
            calculateChimney.setCaseId(operateField.getCaseId());
            chimneyService.chimneySaveData(calculateChimney);
        }

                //更新磨煤机选择
        MillSelect millSelect = millSelectDao.getMillSelect(nature.getPowerCaseId());
        if (millSelect != null) {
            millSelect.setMyglState(operateField.getMyglState());
            millSelect.setE12State(operateField.getE12State());
            millSelect.setE74State(operateField.getE74State());
            millSelect.setE75State(operateField.getE75State());
            millSelect.setModelState(operateField.getModelState());
            millSelectService.chooseDataSave(millSelect);
        }
        //更新煤粉管道计算
        PipeCalculate pipeCalculate = pipeCalculateDao.getPipeCalculate(nature.getPowerCaseId());
        if (pipeCalculate != null) {
            pipeCalculateService.pipelineDataSave(pipeCalculate);
        }

    }

    private void natureSaveData(Nature nature) {
        //计算当地大气压KPa
        if ("0".equals(nature.getLocalPressureState())) {
            nature.setLocalPressure(LocalPressureByFormula21(nature));
        }
        //计算当前大气压mmhg
        if ("0".equals(nature.getLocalPressureOtherState())) {
            nature.setLocalPressureOther(calculateLocalPressureOtherByFormula22(nature));
        }
        //计算空气含湿量
        if ("0".equals(nature.getAnnualAverageAirMoistureState())) {
            nature.setAnnualAverageAirMoisture(calculateAnnualAverageAirMoistureByFormula23(nature));
        }
        //当地湿空气密度
        if ("0".equals(nature.getLocalDensityMoistAirState())) {
            nature.setLocalDensityMoistAir(calculateLocalDensityMoistAirByFormula24(nature));
        }
        //判断新增或者编辑
        Nature oldNature = natureDao.getNatureByPowerCaseId(nature.getPowerCaseId());
        if (oldNature == null) {
            natureDao.addNature(nature);
        } else {
            natureDao.updateNature(nature);
        }
        //更新编辑或者指定状态
        OperateField operateField = new OperateField();
        operateField.setLocalPressureState(nature.getLocalPressureState());
        operateField.setAnnualAverageAirMoistureState(nature.getAnnualAverageAirMoistureState());
        operateField.setLocalPressureOtherState(nature.getLocalPressureOtherState());
        operateField.setLocalDensityMoistAirState(nature.getLocalDensityMoistAirState());
        operateField.setCaseId(nature.getPowerCaseId());
        operateFieldDao.updateOperateFieldNature(operateField);
    }

    private double LocalPressureByFormula21(Nature nature) {
        Formula formula21 = expressionDao.getFormulaByName("公式21");
        Expression expression = new ExpressionImpl(formula21.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E3", nature.getAltitude());
        params.put("E10", nature.getAnnualAverageTemperature());
        double result1 = Double.valueOf(expression.evaluate(params).toString());
        double localPressure = 101.325 / Math.pow(10, result1);
        return DoubleUtil.getInstance().get3Double(localPressure);
    }

    public double calculateLocalPressureOtherByFormula22(Nature nature) {
        Formula formula22 = expressionDao.getFormulaByName("公式22");
        Expression expression = new ExpressionImpl(formula22.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E4", nature.getLocalPressure());
        params.put("E6", 101.325);
        params.put("E7", 760.0);
        return DoubleUtil.getInstance().get3Double(Double.valueOf(expression.evaluate(params).toString()));

    }

    public double calculateAnnualAverageAirMoistureByFormula23(Nature nature) {
        Formula formula23 = expressionDao.getFormulaByName("公式23");
        Expression expression = new ExpressionImpl(formula23.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E4", nature.getLocalPressure());
        params.put("E10", nature.getAnnualAverageTemperature());
        params.put("E13", nature.getAnnualAverageRelativeHumidity());
        double E14 = Double.valueOf(expression.evaluate(params).toString());
        E14 = DoubleUtil.getInstance().get4Double(E14);
        return E14;
    }

    public double calculateLocalDensityMoistAirByFormula24(Nature nature) {
        Formula formula24 = expressionDao.getFormulaByName("公式24");
        Expression expression = new ExpressionImpl(formula24.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E14", nature.getAnnualAverageAirMoisture());
        double LocalDensityMoistAir = Double.valueOf(expression.evaluate(params).toString());
        LocalDensityMoistAir = DoubleUtil.getInstance().get3Double(LocalDensityMoistAir);
        return LocalDensityMoistAir;
    }
}
